package life.system.product.service.impl;

import life.system.product.vo.CityResult;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import life.system.common.utils.PageUtils;
import life.system.common.utils.Query;

import life.system.product.dao.CityDao;
import life.system.product.entity.CityEntity;
import life.system.product.service.CityService;


@Service("cityService")
public class CityServiceImpl extends ServiceImpl<CityDao, CityEntity> implements CityService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CityEntity> page = this.page(
                new Query<CityEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }


    public List<CityResult> lists() {

        List<CityResult> results = new ArrayList<>();

        List<CityEntity> list = this.list();

        Map<String, List<CityEntity>> map = new HashMap<>();

        for (CityEntity city : list) {
            if (map.containsKey(city.getAbbreviation())) {
                map.get(city.getAbbreviation()).add(city);
            } else {
                List<CityEntity> cityEntities = new ArrayList<>();
                cityEntities.add(city);
                map.put(city.getAbbreviation(), cityEntities);
            }
        }

        System.out.println(map);

        for (String s : map.keySet()) {
            CityResult cityResult = new CityResult();
            cityResult.setAbbreviation(s);
            cityResult.setCity(map.get(s));
            results.add(cityResult);
        }

        return results;
    }


}